import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train_exam_appro.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/study/train_exam_appro_list_item_widget.dart';
import 'package:flutter_learn_vi/src/page/train/model/train_model.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/2/18.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///我的审批列表
class TrainExamApproListPage extends StatefulWidget {
  @override
  _TrainExamApproListPageState createState() => _TrainExamApproListPageState();
}

class _TrainExamApproListPageState extends State<TrainExamApproListPage>
    with TrainModel {
  @override
  void initState() {
    super.initState();
    _getTrainExamList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("审批中心"),
      ),

      ///填充布局
      body: RefreshIndicator(
        onRefresh: () async {
          await _getTrainExamList();
          ToastUtils.showToast("已刷新");
          return true;
        },
        child: buildMainWidget(),
      ),
    );
  }

  bool _isLoading = true;
  List<BeanTrainExamAppro> _list = [];

  Future<bool> _getTrainExamList() async {
    _list = await getTrainExamApproList();
    _isLoading = false;
    if (mounted && _list.length > 0) {
      setState(() {});
    }
    return true;
  }

  buildMainWidget() {
    if (_isLoading) {
      return LoadingWidget();
    }

    if (_list.length == 0) {
      return NoDataWidget(
        clickCallBack: () {
          setState(() {
            _isLoading = true;
          });
          _getTrainExamList();
        },
      );
    }

    return ListView.builder(
      itemCount: _list.length,
      itemBuilder: (BuildContext context, int index) {
        BeanTrainExamAppro beanTrainExamAppro = _list[index];
        return TrainExamApproListItemWidget(
            beanTrainExamAppro: beanTrainExamAppro);
      },
    );
  }
}
